package src.com.cdcas.ui;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;


public class GameFrame extends JFrame implements KeyListener, ActionListener {
    //图片更替
    String MMPhoto[]={"..\\JigSaw\\image\\girl\\girl1\\","..\\JigSaw\\image\\girl\\girl2\\","..\\JigSaw\\image\\girl\\girl3\\","..\\JigSaw\\image\\girl\\girl4\\","..\\JigSaw\\image\\girl\\girl5\\",
            "..\\JigSaw\\image\\girl\\girl6\\","..\\JigSaw\\image\\girl\\girl7\\","..\\JigSaw\\image\\girl\\girl8\\","..\\JigSaw\\image\\girl\\girl9\\",
            "..\\JigSaw\\image\\girl\\girl10\\","..\\JigSaw\\image\\girl\\girl11\\","..\\JigSaw\\image\\girl\\girl12\\","..\\JigSaw\\image\\girl\\girl13\\"};
    String animalPhoto[]={"..\\JigSaw\\image\\animal\\animal1\\","..\\JigSaw\\image\\animal\\animal2\\","..\\JigSaw\\image\\animal\\animal3\\","..\\JigSaw\\image\\animal\\animal4\\","..\\JigSaw\\image\\animal\\animal5\\",
            "..\\JigSaw\\image\\animal\\animal6\\","..\\JigSaw\\image\\animal\\animal7\\","..\\JigSaw\\image\\animal\\animal8\\"};
    String sportPhoto[]={"..\\JigSaw\\image\\sport\\sport1\\","..\\JigSaw\\image\\sport\\sport2\\","..\\JigSaw\\image\\sport\\sport3\\","..\\JigSaw\\image\\sport\\sport4\\","..\\JigSaw\\image\\sport\\sport5\\","..\\JigSaw\\image\\sport\\sport6\\",
            "..\\JigSaw\\image\\sport\\sport7\\","..\\JigSaw\\image\\sport\\sport8\\","..\\JigSaw\\image\\sport\\sport9\\","..\\JigSaw\\image\\sport\\sport10\\"};

    //菜单项目 用于行动监听进行事件操作
    JMenuItem replayItem = new JMenuItem("重新游戏");
    JMenuItem reLoginItem = new JMenuItem("重新登录");
    JMenuItem closeItem = new JMenuItem("关闭游戏");
    JMenuItem vxItem = new JMenuItem("微信号");
    JMenuItem MMItem = new JMenuItem("美女");
    JMenuItem animalItem = new JMenuItem("动物");
    JMenuItem sportItem = new JMenuItem("运动");
    //统计步数
    int count = 0;
    //存放胜利的坐标
    int win[][] = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 0}
    };
    //存放路径
    String path = "..\\JigSaw\\image\\animal\\animal3\\";
    //用于存放图片数据
    int[][] data = new int[4][4];
    //存放空白图片坐标
    int x = 0;
    int y = 0;

    public GameFrame() {
        //界面初始化
        initFrame();
        //菜单初始化
        initMenu();
        //数据初始化（打乱图片）
        initData();
        //图片初始化
        initPhoto();

        //设置显示 最后写
        this.setVisible(true);
    }

    private void initData() {

        //打乱顺序
        int[] arr = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
        Random r = new Random();
        for (int i = 0; i < arr.length; i++) {
            int index = r.nextInt(arr.length);
            int temp = arr[i];
            arr[i] = arr[index];
            arr[index] = temp;
        }
        //放入data[][]
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == 0) {
                x = i / 4;
                y = i % 4;
            }
            data[i / 4][i % 4] = arr[i];
        }
    }

    private void initPhoto() {
        //清除原有的图片信息 （更替图片）
        this.getContentPane().removeAll();
        //判断是否胜利
        if (win()) {
            //添加胜利图标
            JLabel winJLabel = new JLabel(new ImageIcon("..\\JigSaw\\image\\win.png"));
            winJLabel.setBounds(203, 283, 197, 73);
            this.getContentPane().add(winJLabel);
        }
        //统计步数
        JLabel stepJLabel = new JLabel("步数：" + count);
        stepJLabel.setBounds(50, 30, 100, 20);
        this.getContentPane().add(stepJLabel);

        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                //获取图片
//              ImageIcon icon=new ImageIcon("D:\\Code\\JigSaw\\image\\animal\\animal3\\1.jpg");
                //创建JLabel  管理容器
                JLabel jLabel = new JLabel(new ImageIcon(path + data[i][j] + ".jpg"));
                //设置图片位置
                jLabel.setBounds(105 * j + 83, 105 * i + 134, 105, 105);
                //添加图片边框
                jLabel.setBorder(new BevelBorder(BevelBorder.LOWERED));
                //添加到界面
                //this.add(jLabel);
                this.getContentPane().add(jLabel);
            }
        }
        //添加背景图片
        JLabel jLabel2 = new JLabel(new ImageIcon("..\\JigSaw\\image\\background.png"));
        jLabel2.setBounds(40, 40, 508, 560);
        this.getContentPane().add(jLabel2);
        //重新进行图片加载 （刷新）
        this.getContentPane().repaint();
    }

    private void initMenu() {
        //菜单栏
        JMenuBar jMenuBar = new JMenuBar();
        //菜单内容
        JMenu functionMenu = new JMenu("功能");
        JMenu aboutMenu = new JMenu("联系方式");
        JMenu changeMenu = new JMenu("更换图片");

        //添加到各个功能
        functionMenu.add(changeMenu);
        functionMenu.add(replayItem);
        functionMenu.add(reLoginItem);
        functionMenu.add(closeItem);

        aboutMenu.add(vxItem);

        changeMenu.add(MMItem);
        changeMenu.add(animalItem);
        changeMenu.add(sportItem);
        //更换图片操作在功能模块中


        //增加行动监听事件
        replayItem.addActionListener(this);
        reLoginItem.addActionListener(this);
        closeItem.addActionListener(this);

        vxItem.addActionListener(this);

        MMItem.addActionListener(this);
        animalItem.addActionListener(this);
        sportItem.addActionListener(this);
        //添加到菜单栏
        jMenuBar.add(functionMenu);
        jMenuBar.add(aboutMenu);
        //设置菜单栏 （菜单栏添加到JFrame）
        this.setJMenuBar(jMenuBar);


    }

    private void initFrame() {
        //设置宽高
        this.setSize(603, 680);
        //设置标题
        this.setTitle("拼图单机版 v1.0");
        //设置页面置顶
        this.setAlwaysOnTop(true);
        //设置页面居中显示
        this.setLocationRelativeTo(null);
        //设置关闭方式
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //取消默认布局
        this.setLayout(null);
        //增加键盘监听事件
        this.addKeyListener(this);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        //查看完整图片功能
        int keyCode = e.getKeyCode();
        //按A查看
        if (keyCode == 65) {
            //删除原有图片
            this.getContentPane().removeAll();
            //添加完整图片
            JLabel wholeJLabel = new JLabel(new ImageIcon(path+"all.jpg"));
            wholeJLabel.setBounds(83, 134, 420, 420);
            this.getContentPane().add(wholeJLabel);
            //添加背景图片
            JLabel jLabel2 = new JLabel(new ImageIcon("..\\JigSaw\\image\\background.png"));
            jLabel2.setBounds(40, 40, 508, 560);
            this.getContentPane().add(jLabel2);
            //刷新界面
            this.getContentPane().repaint();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        int keyCode = e.getKeyCode();
        //判断是否胜利 胜利则结束
        if (win()) {
            return;
        }
        //向左：37 上：38 右：39 下：40
        if (keyCode == 37) {
            System.out.println("向左移动");
            if (y == 3) {
                return;
            }
            //向左移动
            //右边图片与空白互换位置
            data[x][y] = data[x][y + 1];
            data[x][y + 1] = 0;
            y++;
            //增加步数
            count++;
        } else if (keyCode == 38) {
            System.out.println("向上移动");
            if (x == 3) {
                return;
            }
            //原理同上
            data[x][y] = data[x + 1][y];
            data[x + 1][y] = 0;
            x++;
            //增加步数
            count++;
        } else if (keyCode == 39) {
            System.out.println("向右移动");
            if (y == 0) {
                return;
            }
            //原理同上
            data[x][y] = data[x][y - 1];
            data[x][y - 1] = 0;
            y--;
            //增加步数
            count++;
        } else if (keyCode == 40) {
            System.out.println("向下移动");
            if (x == 0) {
                return;
            }
            //原理同上
            data[x][y] = data[x - 1][y];
            data[x - 1][y] = 0;
            x--;
            //增加步数
            count++;
        } else if (keyCode == 87) {
            //按w作弊 直接胜利
            data = new int[][]{
                    {1, 2, 3, 4},
                    {5, 6, 7, 8},
                    {9, 10, 11, 12},
                    {13, 14, 15, 0}
            };
        }
        initPhoto();
    }

    //判断是否胜利
    public boolean win() {

        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                if (win[i][j] != data[i][j]) {
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        Random r=new Random();
        if (source == replayItem) {
            count = 0;
            initData();
            initPhoto();
        } else if (source == reLoginItem) {
            this.setVisible(false);
            new LoginFrame();
        } else if (source == closeItem) {
            System.exit(0);
        } else if (source == vxItem) {
            //设置一个弹窗对象
            JDialog jDialog = new JDialog();
            jDialog.setTitle("微信号");
            JLabel vxJLabel = new JLabel(new ImageIcon("..\\JigSaw\\image\\vx.png"));
            vxJLabel.setBounds(0, 0, 224, 224);
            jDialog.getContentPane().add(vxJLabel);
            jDialog.setSize(325, 325);
            //弹框置顶
            jDialog.setAlwaysOnTop(true);
            //居中
            jDialog.setLocationRelativeTo(null);
            //弹框不处理无法进行其他操作
            jDialog.setModal(true);
            jDialog.setVisible(true);
        } else if (source==MMItem) {
            count=0;
            path=MMPhoto[r.nextInt(MMPhoto.length)];
            initData();
            initPhoto();
        } else if (source==animalItem) {
            count=0;
            path=animalPhoto[r.nextInt(animalPhoto.length)];
            initData();
            initPhoto();
            System.out.println(path);
        }else if (source==sportItem){
            count=0;
            path=sportPhoto[r.nextInt(sportPhoto.length)];
            initData();
            initPhoto();
        }

    }
}
